/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Etbl_Cars;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mr.aUgust
 */
public class DALtbl_Cars extends BaseConnection{
    CallableStatement clbStm=null;
    ResultSet rs=null;
      public boolean addtbl_Cars(Etbl_Cars obj){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Cars_add(?,?,?,?,?,?,?,?,?,?)}");
            clbStm.setString("name", obj.getName());
            clbStm.setInt("seating_capacity",obj.getSeating_capacity());
            clbStm.setBoolean("air_conditioner",obj.isAir_conditioner());
            clbStm.setString("sales_types", obj.getSales_types());
            clbStm.setFloat("sales_values", obj.getSales_values());
            clbStm.setFloat("price", obj.getPrice());
            clbStm.setString("images", obj.getImages());
            clbStm.setInt("carmodel_Id", obj.getCarmodel_Id());
            clbStm.setInt("cartype_Id", obj.getCartype_Id());            
            clbStm.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public boolean edittbl_Cars(Etbl_Cars obj){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Cars_update(?,?,?,?,?,?,?,?,?,?,?)}");
            clbStm.setInt("carId",obj.getCarId());
            clbStm.setString("name", obj.getName());
            clbStm.setInt("seating_capacity",obj.getSeating_capacity());
            clbStm.setBoolean("air_conditioner",obj.isAir_conditioner());
            clbStm.setString("sales_types", obj.getSales_types());
            clbStm.setFloat("sales_values", obj.getSales_values());
            clbStm.setFloat("price", obj.getPrice());
            clbStm.setString("images", obj.getImages());
            clbStm.setInt("carmodel_Id", obj.getCarmodel_Id());
            clbStm.setInt("cartype_Id", obj.getCartype_Id());              
            clbStm.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public boolean deletetbl_Cars(int carId){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Cars_delete(?)}");
            clbStm.setInt("carId",carId);              
            clbStm.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cars.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      public Etbl_Cars getOne_Car(int carId){
          Etbl_Cars oCar=new Etbl_Cars();
        try {
            getConnection();
            clbStm=cnn.prepareCall("{tbl_Cars_get(?)}");
            clbStm.setInt("carId", carId);
            rs=clbStm.executeQuery();
            if(rs.next()){
                oCar.setCarId(carId);
                oCar.setCarmodel_Id(rs.getInt("carmodel_Id"));
                oCar.setCartype_Id(rs.getInt("cartype_Id"));
                oCar.setAir_conditioner(rs.getBoolean("air_conditioner"));
                oCar.setImages(rs.getString("images"));
                oCar.setName(rs.getString("name"));
                oCar.setNameCarmodel(rs.getString("CarModelName"));
                oCar.setNameType(rs.getString("CarTypeName"));
                oCar.setPrice(rs.getFloat("price"));
                oCar.setSales_types(rs.getString("sales_types"));
                oCar.setSales_values(rs.getFloat("sales_values"));
                oCar.setSeating_capacity(rs.getInt("seating_capacity"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cars.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return oCar;
        }
      }
      public ArrayList<Etbl_Cars> getList_Cars(String orderBy, String ASCorDESC, String WhereClause, int PagingIndex, int PagingSize){
        ArrayList<Etbl_Cars> arrCars=new ArrayList<Etbl_Cars>();
          try {
             getConnection();
              clbStm=cnn.prepareCall("{call tbl_Cars_getpaged(?,?,?,?,?)}");
                clbStm.setString("OrderBy", orderBy);
                clbStm.setString("TangGiam", ASCorDESC);
                clbStm.setString("WhereClause", WhereClause);
                clbStm.setInt("PageIndex", PagingIndex);
                clbStm.setInt("PageSize", PagingSize);
                rs=clbStm.executeQuery();
                DALTotalPageIndex.getTotalPage("tbl_Cars", WhereClause, PagingSize);
                while(rs.next()){
                    Etbl_Cars oCar=new Etbl_Cars();
                    oCar.setCarId(rs.getInt("carId"));
                    oCar.setCarmodel_Id(rs.getInt("carmodel_Id"));
                    oCar.setCartype_Id(rs.getInt("cartype_Id"));
                    oCar.setAir_conditioner(rs.getBoolean("air_conditioner"));
                    oCar.setImages(rs.getString("images"));
                    oCar.setName(rs.getString("name"));
                    oCar.setNameCarmodel(rs.getString("Car_Model_Name"));
                    oCar.setNameType(rs.getString("CarTypeName"));
                    oCar.setPrice(rs.getFloat("price"));
                    oCar.setSales_types(rs.getString("sales_types"));
                    oCar.setSales_values(rs.getFloat("sales_values"));
                    oCar.setSeating_capacity(rs.getInt("seating_capacity"));
                    arrCars.add(oCar);
                }
                 clbStm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cars.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return arrCars;
        }
      }
      public ArrayList<Etbl_Cars> getTop10Car(){
          ArrayList arrTopCar=new ArrayList();
          try {
            getConnection();
            clbStm=cnn.prepareCall("{call SelectTop10Car}");
            rs=clbStm.executeQuery();
            while(rs.next()){
                Etbl_Cars objCar=new Etbl_Cars();
                objCar.setCarId(rs.getInt("carId"));
                objCar.setName(rs.getString("name"));
                objCar.setImages(rs.getString("images"));
                arrTopCar.add(objCar);
            }
            rs.close();
            clbStm.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cars.class.getName()).log(Level.SEVERE, null, ex);
        }
          return arrTopCar;
      }
}
